<template>
  <div style="padding: 40px;">
    <a-table
      :loading="isLoading"
      :columns="columns01"
      :data-source="tableData01"
      :rowKey="
        (record, index) => {
          return index;
        }
      "
    >
      <template slot="title">
        开奖结果
      </template>
    </a-table>

  </div>
</template>
<script>
import axios from 'axios'

export default {
  data () {
    return {
      // matchIdList: ['1025732', '1025733', '1025734', '1025735', '1025736', '1025737', '1025738', '1025739', '1025740', '1025741',
      //   '1025742', '1025743', '1025794', '1025795', '1025796', '1025797', '1025798', '1025799', '1025800', '1025801', '1025802',
      //   '1025803', '1025804', '1025805', '1025833', '1025834', '1025835', '1025836'],
      // matchIdList: ['1025732'],
      // 奥运会足球
      matchIdList: ['1026236', '1026237', '1026238', '1026239', '1026240', '1026241', '1026242', '1026243', '1026268',
        '1026269', '1026272', '1026273', '1026277', '1026278', '1026280', '1026281', '1026302'],
      isLoading: false,
      alldatas: {
        objList: []
      },
      CRS: '',
      alldatas02: [],
      tableData01: [],
      columns01: [
        {
          dataIndex: 'index',
          title: '编号',
          slots: { title: 'index' },
          scopedSlots: { customRender: 'index' }
        },
        {
          dataIndex: 'matchId',
          title: '比赛id',
          slots: { title: 'matchId' },
          scopedSlots: { customRender: 'matchId' }
        },
        {
          dataIndex: 'matchDateTime',
          title: '比赛时间',
          slots: { title: 'matchDateTime' },
          scopedSlots: { customRender: 'matchDateTime' }
        },
        {
          dataIndex: 'homeTeamAllName',
          title: '主场',
          slots: { title: 'homeTeamAllName' },
          scopedSlots: { customRender: 'homeTeamAllName' }
        },
        {
          dataIndex: 'awayTeamAbbName',
          title: '客场',
          slots: { title: 'awayTeamAbbName' },
          scopedSlots: { customRender: 'awayTeamAbbName' }
        },
        {
          dataIndex: 'HAD',
          title: '胜平负',
          slots: { title: 'HAD' },
          scopedSlots: { customRender: 'HAD' }
        },
        {
          dataIndex: 'HHAD',
          title: '让球胜平负',
          slots: { title: 'HHAD' },
          scopedSlots: { customRender: 'HHAD' }
        },
        {
          dataIndex: 'CRS',
          title: '比分',
          slots: { title: 'CRS' },
          scopedSlots: { customRender: 'CRS' }
        },
        {
          dataIndex: 'TTG',
          title: '总进球',
          slots: { title: 'TTG' },
          scopedSlots: { customRender: 'TTG' }
        },
        {
          dataIndex: 'HAFU',
          title: '半全场胜平负',
          slots: { title: 'HAFU' },
          scopedSlots: { customRender: 'HAFU' }
        },
        {
          dataIndex: 'sectionsNo999',
          title: '总比分',
          slots: { title: 'sectionsNo999' },
          scopedSlots: { customRender: 'sectionsNo999' }
        }

      ],
      allId: [],
      money: 0,
      capNum: 10,
      score: ''

    }
  },
  mounted () {
    this.isLoading = true
    this.alldatas = {
      objList: []
    }
    this.alldatas02 = []
    this.getAll()
  },
  methods: {
    async getData01 (params) {
      return axios.get('https://webapi.sporttery.cn/gateway/jc/football/getFixedBonusV1.qry', { params: params })
    },
    async getData02 (params) {
      return axios.get('https://webapi.sporttery.cn/gateway/uniform/football/getMatchHeadV1.qry', { params: params })
    },
    async getAll () {
      for (var i = 0; i < this.matchIdList.length; i++) {
        const j = i
        const data01 = localStorage.getItem('matchId:' + this.matchIdList[j])
        if (data01) {
          this.alldatas.objList.push(JSON.parse(data01))
        } else {
          const params = {
            clientCode: 3001,
            matchId: this.matchIdList[j]
          }
          const res = await this.getData01(params)

          localStorage.setItem('matchId:' + params.matchId, JSON.stringify(res))
          this.alldatas.objList.push(res)
        }

        const data02 = localStorage.getItem('sportteryMatchId:' + this.matchIdList[j])
        if (data02) {
          this.alldatas02.push(JSON.parse(data02))
        } else {
          const params = {
            source: 'm',
            sportteryMatchId: this.matchIdList[j]
          }
          const res = await this.getData02(params)
          localStorage.setItem('sportteryMatchId:' + params.sportteryMatchId, JSON.stringify(res))
          this.alldatas02.push(res)
        }
      }
      this.setData()
      // 查看比分奖励
      this.showStatus()

      // 计算策略结果
      // this.getMaxNum()
      this.isLoading = false
    },
    // 获取最大奖励的策略
    getMaxNum () {
      console.log('策略是：选取赔率最低的前' + this.capNum + '个值')
      this.alldatas.objList.forEach(item => {
        this.score = item.data.value.sectionsNo999
        const datList = item.data.value.oddsHistory.crsList
        this.money += this.capNum * 10
        console.log('买入' + item.data.value.homeTeamShortName + ':' + item.data.value.awayTeamShortName + '的比赛，当前费用：' + this.money)
        const dat = datList[datList.length - 1]
        const kyes = Object.keys(dat)
        const lists = []
        kyes.forEach(item => {
          lists.push({
            name: item,
            value: dat[item]
          })
        })
        const newLists = lists.filter(item => {
          return item.name.includes('s0') && !item.name.includes('f')
        })
        newLists.sort((a, b) => {
          const x = a.value - 0
          const y = b.value - 0
          return x - y
        })
        this.showNum02(newLists)
      })
      console.log('最终使用的费用是：' + this.money)
    },
    showNum02 (list) {
      list.forEach((item, index) => {
        if (index <= this.capNum) {
          if (this.score === (item.name[2] + ':' + item.name[5])) {
            this.money -= 10 * item.value
            console.log('恭喜中奖，当前费用：' + this.money)
          }
        }
      })
      console.log('------------------------------------------')
    },
    // 显示最低赔率炒股情况
    showStatus () {
      this.alldatas.objList.forEach(item => {
        console.log('当前队伍是：' + item.data.value.homeTeamShortName + ':' + item.data.value.awayTeamShortName)
        console.log('总比分是：' + item.data.value.sectionsNo999 + '，赔率是：' + item.data.value.matchResultList[2].odds)
        const datList = item.data.value.oddsHistory.crsList
        const dat = datList[datList.length - 1]
        const kyes = Object.keys(dat)
        const lists = []
        kyes.forEach(item => {
          lists.push({
            name: item,
            value: dat[item]
          })
        })
        const newLists = lists.filter(item => {
          return item.name.includes('s0') && !item.name.includes('f')
        })
        newLists.sort((a, b) => {
          const x = a.value - 0
          const y = b.value - 0
          return x - y
        })
        console.log('赔率最低的6个比分：')
        this.showNum(newLists)
      })
    },
    showNum (list) {
      list.forEach((item, index) => {
        if (index <= 5) {
          console.log('比分是：' + item.name[2] + ':' + item.name[5] + ',赔率是：' + item.value)
        }
      })
      console.log('------------------------------------------')
    },
    setData () {
      this.alldatas.objList.forEach(item => {
        const id = item.data.value.matchResultList[0].matchId
        const index = this.alldatas02.findIndex(item02 => item02.data.value.sportteryMatchId === id)
        Object.assign(this.alldatas.objList[index].data.value, this.alldatas02[index].data.value)
      })

      this.allId = []
      this.alldatas.objList.forEach((item, index) => {
        const num = Math.random()
        const obj = {
          key: num,
          index: index + 1,
          matchId: item.data.value.matchResultList[0].matchId,
          matchDateTime: item.data.value.matchDateTime,
          homeTeamAllName: item.data.value.oddsHistory.homeTeamAllName,
          awayTeamAbbName: item.data.value.oddsHistory.awayTeamAbbName,
          sectionsNo999: item.data.value.sectionsNo999
        }
        this.allId.push(item.data.value.matchResultList[0].matchId)
        item.data.value.matchResultList.forEach((item, index) => {
          const a = item.combinationDesc
          const b = item.odds

          obj[item.code] = `${a}(奖励：${b})`
          // obj.HHAD = a + '--' + b // 让球胜平负
          // obj.HAFU = a + '--' + b // 半全场胜平负
          // obj.CRS = a + '--' + b // 比分
          // obj.TTG = a + '--' + b // 总进球
          // obj.HAD = a + '--' + b // 胜平负
          if (item.code === 'CRS') {
            this.CRS = a
          }
        })
        this.tableData01.push(obj)
      })
    }
  }
}
</script>
